'use strict';
app.service('modulesGoodsService',['$q','utilService',function($q,utilService){
    var _this_=this;

    //移除模块商品
    _this_.delModulesGoods= function ($scope,id) {
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'modules/goods',
            data:{id:id},
            method:'DELETE'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //下移模块商品
    _this_.sortDown= function ($scope,id) {
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'modules/goods/sort/down',
            data:{id:id,modulesId:$scope.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //上移模块商品
    _this_.sortUp= function ($scope,id) {
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'modules/goods/sort/up',
            data:{id:id,modulesId:$scope.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //置顶模块商品
    _this_.sortStick= function ($scope,id) {
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'modules/goods/sort/stick',
            data:{id:id,modulesId:$scope.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //上下架
    _this_.updateStatus= function ($scope,id) {
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'modules/goods/status',
            data:{id:id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //批量上下架
    _this_.batchUpdateStatus= function ($scope,status) {
        var ids=new Array();
        for(var i=0;i<$scope.selected.length;i++){
            ids.push($scope.selected[i].id);
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'modules/goods/batch/status',
            data:{ids:ids,status:status},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
}]);